package com.wf.course.web.controller.member;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.utils.page.PageBean;
import com.wf.course.model.log.SysOperateLog;
import com.wf.course.model.member.Recharge;
import com.wf.course.service.IRechargeService;
import com.wf.course.web.Interceptor.LogInfo;

/**
 * 铜钱充值奖励配置
 * @author TT
 *
 */
@RequestMapping("Recharge")
@Controller
public class RechargeController {
	
	@Autowired
	private IRechargeService rechargeService;
	
	/**
	 * 获取铜钱充值活动列表
	 * @param pageNo
	 * @param pageSize
	 * @param title
	 * @param model
	 * @return
	 */
	@LogInfo("获取铜钱充值活动列表")
	@RequestMapping("RechargeList")
	public String RechargeList(@RequestParam(value="pageNo",required=false) Integer pageNo,
							   @RequestParam(value="pageSize",required=false) Integer pageSize,
							   @RequestParam(value="title",required=false) String title,
							   Model model){
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("title", title);
		PageBean<Recharge> pageBean = new PageBean<>(pageNo,pageSize,null,map);
		pageBean.setUrl("Recharge/RechargeList");
		
		rechargeService.getRechargeList(pageBean);		
		
		model.addAttribute("page",pageBean);
		return "member/rechargeList";
	}
	
	/**
	 * 新增/修改铜钱充值活动，页面数据初始化
	 * @return
	 */
	@LogInfo("新增/修改铜钱充值活动，页面数据初始化")
	@RequestMapping("RechargeLoad/{id}")
	public String RechargeLoad(@PathVariable("id") Integer id,Model model){
		if (id != null && id != 0) {
			//修改
			Recharge recharge = rechargeService.getRechargeById(id);
			model.addAttribute("page",recharge);
			model.addAttribute("type","upd");
			
		}else {
			//新增
			model.addAttribute("type","add");
		}
		return "member/rechargeEdit";
	}
	
	/**
	 * 新增/修改铜钱充值活动
	 * @param question
	 * @param request
	 * @return
	 */
	@LogInfo("新增/修改铜钱充值活动")
	@RequestMapping("RechargeOption")
	public String RechargeOption(@ModelAttribute Recharge recharge,
			HttpServletRequest request){
		//添加
		if (recharge.getId() == null) {
			//保存
			SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			recharge.setCreate_time(simpleDateFormat.format(new Date()));
			rechargeService.saveRecharge(recharge);
		}else{
			rechargeService.updateRecharge(recharge);
		}
		
		return "redirect:/Recharge/RechargeList";
	}
	
	/**
	 * 铜钱充值活动
	 * @return
	 */
	@LogInfo("删除铜钱充值活动")
	@RequestMapping("RechargeDel/{id}")
	public String RechargeDel(@PathVariable("id") Integer id){
		rechargeService.deleteRecharge(id);
		return "redirect:/Recharge/RechargeList";
	}
}
